var table;
var layer;
layui.use(['layer','table'],function(){
    table=layui.table;
    layer=layui.layer;
});


//课程分类
selProductCategory()
function selProductCategory() {
    $.ajax({
        url: "/SyOperationController/selCourseClass",
        type: "get",
        dataType: "json",
        success: function (data) {
            shouZTree(data.data);
        }
    });
};

//zTree
function shouZTree(zNodes) {
    var setting = {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "level"
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view:{
            showIcon: false,
        },
        callback: {
            beforeCheck: beforeCheck,
            onCheck: onCheck,
            beforeClick: zTreeBeforeClick
        }
    };

    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
        return !treeNode.isParent;//当是父节点 返回false 不让选取
    };

    var code;
    function setCheck() {
        var type = $("#level").attr("checked")? "level":"all";
        setting.check.radioType = type;
        showCode('setting.check.radioType = "' + type + '";');
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    }
    function showCode(str) {
        if (!code) code = $("#code");
        code.empty();
        code.append("<li>"+str+"</li>");
    }

    $(document).ready(function(){
        setCheck();
        $("#level").bind("change", setCheck);
        $("#all").bind("change", setCheck);
    });


    var code, log, className = "dark";
    function beforeCheck(treeId, treeNode) {
        className = (className === "dark" ? "":"dark");
        showLog("[ "+getTime()+" beforeCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
        return (treeNode.doCheck !== false);
    }



    function onCheck(e, treeId, treeNode) {
        //判断是否是根节点
        if(treeNode.pId!=null){
            $.ajax({
                url: "/SyOperationController/selCousrseChildParent",
                type: "post",
                dataType: "json",
                data:{"id":treeNode.pId},
                success: function (data) {

                    //获取根节点
                    if(data.data.parent){
                        $("#p_id span").replaceWith("<span>"+data.data.parent.name+" > "+data.data.name+" > "+treeNode.name+"</span>");

                        parent.$("#p_id span").replaceWith("<span>"+data.data.parent.name+" > "+data.data.name+" > "+treeNode.name+"</span>");
                        parent.$("input[name=className]").val(treeNode.name);
                        parent.$("input[name=classId]").val(treeNode.id);
                    }else{
                        $("#p_id span").replaceWith("<span>"+data.data.name+" > "+treeNode.name+"</span>");

                        parent.$("#p_id span").replaceWith("<span>"+data.data.name+" > "+treeNode.name+"</span>");
                        parent.$("input[name=className]").val(treeNode.name);
                        parent.$("input[name=classId]").val(treeNode.id);
                    }
                }
            });
        }else{
            $("#p_id span").replaceWith("<span>"+treeNode.name+"</span>");
            parent.$("#p_id span").replaceWith("<span>"+treeNode.name+"</span>");
            parent.$("input[name=className]").val(treeNode.name);
            parent.$("input[name=classId]").val(treeNode.id);
        }

        showLog("[ "+getTime()+" onCheck ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name );
    }



    function showLog(str) {
        if (!log) log = $("#log");
        log.append("<li class='"+className+"'>"+str+"</li>");
        if(log.children("li").length > 6) {
            log.get(0).removeChild(log.children("li")[0]);
        }
    }


    function getTime() {
        var now= new Date(),
            h=now.getHours(),
            m=now.getMinutes(),
            s=now.getSeconds(),
            ms=now.getMilliseconds();
        return (h+":"+m+":"+s+ " " +ms);
    }


}